package cn.jiangjiesheng.bootstrap.integration.datapool;

import cn.jiangjiesheng.bootstrap.commons.annotation.IgnoreResponseWrapper;
import cn.jiangjiesheng.bootstrap.integration.datapool.params.ReqActionEnum;
import cn.jiangjiesheng.bootstrap.integration.datapool.params.ReqActionParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/datapool")
@Slf4j
@DependsOn("jdbcTemplateForPaas")
public class DataPoolIntegrationEndpoint {

    private final ReadService readService;
    private final WriteService writeService;

    public DataPoolIntegrationEndpoint(ReadService readService, WriteService writeService) {
        this.readService = readService;
        this.writeService = writeService;
    }


    @PostMapping("/action")
    @IgnoreResponseWrapper
    public Object action(@RequestBody ReqActionParam reqActionParam) {
        if (reqActionParam.getReqAction() == ReqActionEnum.READ) {
            return readService.read(reqActionParam.getReadActionParam());
        } else {
            return writeService.write(reqActionParam.getWriteActionParam());
        }
    }

}
